package com.rhok.wthru_client.servermsgs;

import android.os.AsyncTask;


/**
 * 
 * Utility class for sending messages to the server asynchronously
 * Example usage:
 * <code>
 * ServerMessageSomething msg = new ServerMessageSomething(getApplicationContext());
 * msg.setParam1(param1);
 * msg.setParam2(param2);
 * ServerMessageOpaque opq1 = new ServerMessageOpaque(msg, new OnTaskCompleted<String>() {
 *
 *		@Override
 *		public void onTaskCompleted(String returnValue) {
 *			// TODO Auto-generated method stub			
 *		}		
 * });
 * ServerMessageOpaque opq2 = new ServerMessageOpaque(msg);
 * new ServerMessageTask().execute(opq1, opq2, ...);
 * </code>
 * 
 * @see ServerMessage
 *
 */
public class ServerMessageTask extends AsyncTask<ServerMessageOpaque, Integer, Integer> {

	@Override
	protected Integer doInBackground(ServerMessageOpaque... msgs) {
		int processed = 0;
		for (int i = 0; i < msgs.length; ++i) {
			String res = msgs[i].msg.send();
			if (msgs[i].listener != null) {
				msgs[i].listener.onTaskCompleted(res);
			}
			++processed;
		}
		
		return processed;
	}

	protected void onPostExecute(Integer result) {

	}

}
